package oocl.ita.eordering.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

public class UploadFileUtil {

	private static Map<String, String> map = new HashMap<>();

	public static Map<String, String> GetPathMap(List<MultipartFile> files, HttpServletRequest request)
			throws IllegalStateException, IOException {
		String mTel = (String) request.getAttribute("mTel");
		String fName = (String) request.getAttribute("fName");
		String fPrice = (String) request.getAttribute("fPrice");
		File dir = null;
		String fViewPath = null;
		String mCardPath = null;
		String mLogoPath = null;
		for (MultipartFile file : files) {
			String fileName = file.getOriginalFilename();
			if (fileName.lastIndexOf(".") == -1) {
				System.out.println("文件格式错误！！！");
				continue;
			}
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			switch (file.getName()) {
			case "fViewPath":
				dir = new File("D:/MerchantUploadFiles/" + mTel + "/Foods");
				if (!dir.exists()) {
					dir.mkdirs();
				}
				fViewPath = mTel + "/Foods/" + fName + fPrice + suffix;
				file.transferTo(new File("D:/MerchantUploadFiles/" + fViewPath));
				map.put("fViewPath", fViewPath);
				break;
			case "mCardPath":
				dir = new File("D:/MerchantUploadFiles/" + mTel);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				mCardPath = mTel + "/IDCard" + suffix;
				file.transferTo(new File("D:/MerchantUploadFiles/" + mCardPath));
				map.put("mCardPath", mCardPath);
				break;
			case "mLogoPath":
				dir = new File("D:/MerchantUploadFiles/" + mTel);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				mLogoPath = mTel + "/LOGO" + suffix;
				file.transferTo(new File("D:/MerchantUploadFiles/" + mLogoPath));
				map.put("mLogoPath", mLogoPath);
				break;
			default:
				System.out.println("表单字段错误");
				break;
			}
		}

		return map;
	}
}
